package logic;

import java.io.PrintWriter;
import java.lang.reflect.Field;

public abstract class Logic {

	public static boolean isSimpleType(Object value) {
		boolean isSimple;
		isSimple = value instanceof String || value instanceof Number
				|| value instanceof Character || value instanceof Boolean;
		return isSimple;
	}

	public static boolean isSimpleClass(String name) {
		boolean isSimple = (name.equals(String.class.getName())
				|| name.equals(Byte.class.getName())
				|| name.equals(Short.class.getName())
				|| name.equals(Integer.class.getName())
				|| name.equals(Long.class.getName())
				|| name.equals(Float.class.getName())
				|| name.equals(Double.class.getName())
				|| name.equals(Boolean.class.getName()) || name
				.equals(Character.class.getName()));
		return isSimple;
	}

	public static void silentClose(PrintWriter pw) {
		if (pw != null) {
			pw.close();
		}
	}

	public static void setField(Object object, Field f, String value)
			throws IllegalAccessException {
		String typeName;
		typeName = f.getType().getName();
		if (typeName.equals(String.class.getName())) {
			f.set(object, value);
		}
		if (typeName.equals(Byte.class.getName())) {
			f.set(object, Byte.parseByte(value));
		}
		if (typeName.equals(Short.class.getName())) {
			f.set(object, Short.parseShort(value));
		}
		if (typeName.equals(Integer.class.getName())) {
			f.set(object, Integer.parseInt(value));
		}
		if (typeName.equals(Long.class.getName())) {
			f.set(object, Long.parseLong(value));
		}
		if (typeName.equals(Float.class.getName())) {
			f.set(object, Float.parseFloat(value));
		}
		if (typeName.equals(Double.class.getName())) {
			f.set(object, Double.parseDouble(value));
		}
		if (typeName.equals(Boolean.class.getName())) {
			f.set(object, Boolean.parseBoolean(value));
		}
		if (typeName.equals(Character.class.getName())) {
			f.set(object, value.charAt(0));
		}
	}

}
